/**
 * Project Name:HomeActivity
 * Date:2016-1-13下午12:14:46
 * Copyright (c) 2016, askzhao All Rights Reserved.
 *
 */
package com.kkd.kkdapp.db.user.model;

import java.util.List;

import android.content.ContentValues;

import com.kkd.kkdapp.db.annotate.EADBField;
import com.kkd.kkdapp.db.annotate.EADBField.EADBFieldType;
import com.kkd.kkdapp.db.base.EABaseModel;
import com.kkd.kkdapp.db.base.SqlBuilder;
import com.kkd.kkdapp.db.user.KkdDBHelper;

/**
 * ClassName: CrediItemTab <br/>
 * date: 2016-1-13 下午12:14:46 <br/>
 *
 * @author askzhao
 * @version 
 */
public class CrediItemTab extends EABaseModel {
	
	private static final String TABLE_NAME = CrediItemTab.class.getSimpleName();
	
	@EADBField (type = EADBFieldType.Integer)
	public long payDate; 
	
	@EADBField (type = EADBFieldType.Integer)
	public long amount; 
	
	@EADBField (type = EADBFieldType.Integer)
	public int lilv;
	
	@EADBField (type = EADBFieldType.Integer)
	public long currentPeriodAmount;
	
	@EADBField (type = EADBFieldType.Integer)
	public long totalPayAmount;
	
	@EADBField (type = EADBFieldType.Integer)
	public int periods;
	
	@EADBField (type = EADBFieldType.Text)
	public String status;
	
	@EADBField (type = EADBFieldType.Text)
	public String bidId;
	
	/**
	 * 查询记录 根据bidId
	 * 
	 * @param bidId
	 * @return
	 */
	public static CrediItemTab queryBybidId(String bidId) {
		return KkdDBHelper.getInstance().queryTopOne(CrediItemTab.class, "bidId=? ",
				new String[] { bidId });
	}

	/**
	 * 获取收件箱信息
	 * 
	 * @return
	 */
	public static List<CrediItemTab> queryCrediItemTabList(){

		return KkdDBHelper.getInstance().queryAll(CrediItemTab.class, null, null,
				" first_time desc ", null);

	}

	/**
	 * 删除某个聊天
	 * 
	 * @param mailId
	 * @param mailType
	 * @return
	 */
	public static boolean deleteCrediItemTab(String bidId) {
		return KkdDBHelper.getInstance().delete(TABLE_NAME, " bidId=? ",
				new String[] { bidId }) > 0;
	}

	/**
	 * 清空所有聊天信息
	 * 
	 * @return
	 */
	public static boolean deleteAllMsg() {
		return KkdDBHelper.getInstance().delete(TABLE_NAME, null, null) > 0;
	}
	
	public static boolean updateStrangerLimit(String bidId, String limit) {
		ContentValues values = new ContentValues();
		values.put("extra_1", limit);
		return KkdDBHelper.getInstance().update(TABLE_NAME, values, " bidId = ? ",
				new String[] { bidId }) > 0;
	}
	
	/**
	 * 保存或更新记录
	 * 
	 * @return
	 */
	public static long saveOrUpdate(CrediItemTab info) {
		if (info == null)
			return -1l;

		CrediItemTab dbmail = queryBybidId(info.bidId);
		if (dbmail == null) {
			return KkdDBHelper.getInstance().saveBindId(info);
		} else {
			ContentValues values = SqlBuilder.getContentValuesByObj(info);
			KkdDBHelper.getInstance().update(TABLE_NAME, values, "bidId=?",
					new String[] { info.bidId });
			return 0l;
		}
	}

	/**
	 * 保存但不更新记录
	 * 
	 * @return
	 */
	public static long saveButUpdate(CrediItemTab info) {
		if (info == null)
			return -1l;

		CrediItemTab dbmail = queryBybidId(info.bidId);
		if (dbmail == null) {
			return KkdDBHelper.getInstance().saveBindId(info);
		}
		return 0l;
	}
}
